from openpyxl import load_workbook
import yaml
import pymysql

class ReadData:
    @staticmethod
    def read_excel(filepath, sheet):
        wb = load_workbook(filepath)
        sw = wb[sheet]
        data = [[sw.cell(row=i, column=j).value for j in range(1, sw.max_column + 1)] for i in range(1, sw.max_row + 1)]
        return data

    @staticmethod
    def read_yaml(filename):
        file = open(filename, mode='rb')
        data = yaml.load(file, Loader=yaml.SafeLoader)
        return data

    @staticmethod
    def read_mysql(host='localhost', port=3306, user='root', password='root', db='test', charset='utf8', element='*', table=None, condition=None):
        con = pymysql.connect(host=host, port=port, user=user, password=password, db=db, charset=charset)
        cur = con.cursor()
        sql = f'select {element} from {table} where {condition}'
        cur.execute(sql)
        data = cur.fetchall()
        cur.close()
        con.close()
        return data


